<?php

namespace Common\Model;

use Think\Model;

class VTaskQuotaModel extends Model
{
    public function get($id)
    {
        $result = $this->where('id="%d"', $id)->find();
        return $result;
    }

    public function group_name($task_id)
    {
        $condition = array();
        $condition['status'] = array('eq', 1);
        $condition['id'] = array('eq', $task_id);
        $result = $this->where($condition)->field('name')->group('name')->select();
        return $result;
    }

    public function group_task_quota_content($task_id, $name)
    {
        $condition = array();
        $condition['status'] = array('eq', 1);
        $condition['id'] = array('eq', $task_id);
        $condition['name'] = array('eq', $name);
        $result = $this->where($condition)->field('task_quota_content')->group('task_quota_content')->select();
        return $result;
    }

    public function get_require($task_id, $task_quota_content)
    {
        $condition = array();
        $condition['status'] = array('eq', 1);
        $condition['id'] = array('eq', $task_id);
        $condition['task_quota_content'] = array('eq', $task_quota_content);
        $result = $this->where($condition)->field('task_quota_id,require')->select();
        return $result;
    }

    public function get_list($task_id, $status)
    {
        $condition = $this->assembleCondition($task_id, $status);
        $list = $this->where($condition)->order('id asc')->select();
        return $list;
    }

    private function assembleCondition($task_id, $status)
    {
        $condition = array();

        if (!empty($task_id)) {
            $condition['id'] = array('eq', $task_id);
        }

        if ($status != "" && $status != null) {
            $condition['status'] = array('eq', $status);
        } else {
            $condition['status'] = array('egt', 0);
        }

        return $condition;
    }
}